package com.codingfuture.glacierhardware.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codingfuture.glacierhardware.dto.HostDTO;
import com.codingfuture.glacierhardware.entity.Host;
import com.codingfuture.glacierhardware.mapper.HostMapper;
import com.codingfuture.glacierhardware.service.HostService;
import com.codingfuture.glacierhardware.vo.CompanyVO;
import com.codingfuture.glacierhardware.vo.WareHouseVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class HostServiceImpl implements HostService {
    @Autowired
    private HostMapper hostMapper;

    public Page<Host> getHost(String name, Page<Host> page) {
        QueryWrapper<Host> qw = new QueryWrapper<>();
        qw.like(!(name == null || "".equals(name)), "name", name);
        Page<Host> hostPage = hostMapper.selectPage(page, qw);
        return hostMapper.selectPage(page, qw);
    }

    @Override
    public int addOrUpHost(HostDTO dto) {
        Host host = dto.convert2Entity();
        if (ObjectUtils.isEmpty(dto.getId())) {
            host.setCreateTime(new Date());
            return hostMapper.insert(host);
        } else {
            host.setUpdateTime(new Date());
            return hostMapper.updateById(host);
        }
    }

    @Override
    public List<CompanyVO> getCompanyName() {
        return hostMapper.getCompanyName();
    }

    @Override
    public List<WareHouseVO> getWarehouseName(Integer companyId) {
        return hostMapper.getWarehouseName(companyId);
    }

    @Override
    public int delHost(Long id) {
        return hostMapper.deleteById(id);
    }
}